package com.zttek.photosShareProject.modules.service.manager.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zttek.photosShareProject.common.myException.DBException;
import com.zttek.photosShareProject.modules.mapper.AdminMapper;
import com.zttek.photosShareProject.modules.model.Admin;
import com.zttek.photosShareProject.modules.service.manager.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by 13296 on 2017/8/14.
 */
@Service
public class AdminServiceImpl implements IAdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public List<Admin> selectAllAdmin() throws Exception {
        EntityWrapper<Admin> ew = new EntityWrapper<>();
        ew.where("valid=1");
        List list = adminMapper.selectList(ew);
        return list;
    }

    @Override
    public Admin selectAdminByAccount(String account) throws Exception {
        Admin admin = new Admin();
        admin.setAccount(account);
        admin.setValid(1);
        admin = adminMapper.selectOne(admin);
        if (admin == null) {
            throw new DBException("该账户不存在！");
        }
        return admin;
    }

    @Override
    public Admin selectAdminByID(Integer id) throws Exception {
        Admin admin = adminMapper.selectById(id);
        if (admin == null) {
            throw new DBException("该账户不存在！");
        }
        return admin;
    }

    @Transactional
    @Override
    public void insertAdmin(Admin admin) throws Exception {
        adminMapper.insert(admin);
    }

    @Transactional
    @Override
    public void updateAdmin(Admin admin) throws Exception {
        adminMapper.updateById(admin);
    }

    @Transactional
    @Override
    public void deleteAdminById(Integer id) throws Exception {
        adminMapper.deleteById(id);
    }

}
